package com.itheima.manager.dao;

import com.itheima.manager.domain.DitieZhan;
import com.itheima.manager.domain.ManagerMan;

import java.io.*;
import java.util.ArrayList;

public class DiTieDao {
    private static ArrayList<DitieZhan> dtlist = new ArrayList<DitieZhan>();
    private static ArrayList<ManagerMan> mglist = new ArrayList<ManagerMan>();

    static {
        //加载地铁线路
        load01();
    }


    //将文件中的地铁线路加载到集合中去
    public static void load01() {
        BufferedReader brd = null;
        BufferedReader brm = null;
        try {
            brd = new BufferedReader(new FileReader("北京地铁管理系统\\subways.txt"));
            brm = new BufferedReader(new FileReader("北京地铁管理系统\\managers.txt"));
            String diTie, manager;
            while ((diTie = brd.readLine()) != null) {
                if(!diTie.contains(",")){
                    break;
                }
                String[] split = diTie.split(",");
                dtlist.add(new DitieZhan(split[0], split[1], split[2]));
            }
            while ((manager = brm.readLine()) != null) {
                if(!manager.contains(",")){
                    break;
                }
                String[] split = manager.split(",");
                mglist.add(new ManagerMan(split[0], split[1], split[2], split[3]));
            }
            brd.close();
            brm.close();
        } catch (IOException e) {
            //e.printStackTrace();
        }
    }


    //将集合中的地铁线路保存到文件中去
    public static void reSave01() {
        try {
            BufferedWriter bwdt = new BufferedWriter(new FileWriter("北京地铁管理系统\\subways.txt"));
            BufferedWriter bwmg = new BufferedWriter(new FileWriter("北京地铁管理系统\\managers.txt"));
            for (DitieZhan ditieZhan : dtlist) {
                bwdt.write(ditieZhan.toString());
                bwdt.newLine();
            }
            for (ManagerMan managerMan : mglist) {
                bwmg.write(managerMan.toString());
                bwmg.newLine();
            }
            bwdt.close();
            bwmg.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //查找地铁信息
    public ArrayList<DitieZhan> selectDiTieZhan() {
        //下列两行目的是为了保证当查找时数据被修改,文件中内容也更新
        dtlist.clear();
        load01();
        return dtlist;
    }

    //增加一条地铁信息
    public boolean addDiTieZhan(DitieZhan dt) {
        dtlist.add(dt);
        reSave01();
        return true;
    }

    //删除地铁信息
    public void deleteDiTieZhan(String id) {
        int index=getDiTieIdIndex(id);
        dtlist.remove(index);
        reSave01();
    }

    //返回地铁id对应集合的索引
    private int getDiTieIdIndex(String id) {
        int index=-1;
        for (int i = 0; i < dtlist.size(); i++) {
            if(dtlist.get(i)!=null&&dtlist.get(i).getId().equals(id)){
                index=i;
            }
        }
        return index;
    }

    //修改地铁信息
    public void updateDiTieZhan(String id,DitieZhan ditieZhan) {
        int index=getDiTieIdIndex(id);
        dtlist.set(index,ditieZhan);
        reSave01();;
    }
}
